package com.geor.gcr.idc;

import android.Manifest;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

import com.geor.scan.IScanModuleCallBack;
import com.geor.scan.ImgScan;
import com.geor.scan.ImgScanConfiguration;
import com.geor.scan.ui.CaptureActivity;

import org.nutz.json.Json;

import java.util.Map;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void openCF(View view) {
        commonInit("将身份证四角对其放入框内扫描~", "SFZ:CF", R.id.decodeCF, new CommonCallBack());
    }

    public void openCR(View view) {
        commonInit("将身份证四角对其放入框内扫描~", "SFZ:CR", R.id.decodeCR, new CommonCallBack());
    }

    public void openYHK(View view) {
        commonInit("将银行卡四角对其放入框内扫描~", "YHK", R.id.decodeYHK, new ShowCallBack());
    }

    public void openYYZZ(View view) {
        commonInit("将营业执照四角对其放入框内扫描~", "YYZZ", R.id.decodeYYZZ, new CommonCallBack());
    }

    private void commonInit(String title, String request, int idType, IScanModuleCallBack callBack) {
        if (applyWritePermission()) {
            ImgScanConfiguration configuration = new ImgScanConfiguration.Builder(this)
                    .setTitleHeight(53)
                    .setTitleText("扫一扫")
                    .setTitleTextSize(18)
                    .setTitleTextColor(R.color.white)
                    .setTipText(title)
                    .setTipTextSize(14)
                    .setTipMarginTop(40)
                    .setTipTextColor(R.color.white)
                    .setSlideIcon(R.mipmap.capture_add_scanning)
                    .setAngleColor(R.color.white)
                    .setMaskColor(R.color.black_80)
                    .setScanFrameRectRate((float) 0.8)
                    .build();
            ImgScan.getInstance().init(configuration);

            ImgScan.getInstance().launchScan(MainActivity.this, new ImageParserImpl(), new RecognizerImpl(request), idType, callBack);
        }
    }

    private class CommonCallBack implements IScanModuleCallBack {
        @Override
        public void OnReceiveDecodeResult(final Context context, final Object result) {
            final CaptureActivity mCaptureContext = (CaptureActivity) context;
            final Map<String, String> map = (Map<String, String>) result;
            AlertDialog dialog = new AlertDialog.Builder(mCaptureContext)
                    .setMessage(Json.toJson(map))
                    .setCancelable(false)
                    .setNegativeButton("重新扫描", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                            ImgScan.getInstance().restartScan(mCaptureContext);
                        }
                    })
                    .setPositiveButton("继续", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                            ImgScan.getInstance().finishScan(mCaptureContext);
                        }
                    })
                    .create();
            dialog.show();
        }
    }

    private class ShowCallBack implements IScanModuleCallBack {
        @Override
        public void OnReceiveDecodeResult(final Context context, final Object result) {
            final CaptureActivity mCaptureContext = (CaptureActivity) context;
            final Map<String, String> map = (Map<String, String>) result;
            Intent intent = new Intent(context, ImageActivity.class);
            intent.setClass(mCaptureContext, ImageActivity.class);
            Bundle bundleSimple = new Bundle();
            for (String key : map.keySet()) {
                System.out.println(key + "-->" + map.get(key));
                bundleSimple.putString(key, map.get(key));
            }
            intent.putExtras(bundleSimple);
            startActivity(intent);
        }
    }


    public boolean applyWritePermission() {
        String[] permissions = {Manifest.permission.WRITE_EXTERNAL_STORAGE};
        // SDK版本在23以上
        if (Build.VERSION.SDK_INT >= 23) {
            int check = ContextCompat.checkSelfPermission(this, permissions[0]);
            // 权限是否已经 授权 GRANTED---授权  DINIED---拒绝
            if (check == PackageManager.PERMISSION_GRANTED) {
                //调用相机
                return true;
            } else {
                requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
                return false;
            }
        }
        return true;
    }


}
